home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 11.5 KB | 333 lines |
- /*Timestamp object with free Format output.
- */
-
- package symantec.itools.awt;
- import java.util.*;
- import java.awt.*;
- import symjava.sql.Timestamp;
- public class Tstamp extends TextField{
-
- protected int day=1;
- protected int month=0;
- protected int year=0;
- protected int hour=0;
- protected int minute=0;
- protected int second=0;
- protected int nanosec=0;
-
- private String m_DisplayFormat="D* M* D%TH,Y* H%:m%:s%,n3 AM";
- private String m_EntryFormat="MDYhmsn";
-
- private String DAY[]={"DD","D%","D+","D*","TH"};
- private String MON[]={"MM","M%","M+","M*"};
- private String YEA[]={"YY","Y*"};
- private String HOU[]={"HH","H%","hh","h%","AM"};
- private String MIN[]={"mm","m%"};
- private String SEC[]={"ss","s%"};
- private String NAN[]={"n"};
- private String nMON[]={"Jan.","Feb.","Mar.","Apr.","May ","Jun.","Jul.","Aug.","Sep.","Oct.","Nov.","Dec."};
- private String NMON[]={"January","February","March","April","May","June","July","August","September",
- "October","November","December"};
- private String nDAY[]={"Sun.","Mon.","Tue.","Wed.","Thu.","Fri.","Sat."};
- private String NDAY[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
-
- String WAYS[]={"DDMMYY",
- "D% M% YY",
- "D%/M%/Y*",
- "Y*-MM-DD",
- "DD/MM/YY",
- "D%-M+-YY",
- "D% M* Y*",
- "D+ D% M+ Y*",
- "D* D% M+ Y*",
- "M* D% YY",
- "D* M* D% Y*",
- "<< M%.D%.YY >>",
- "Today is D* the D% th of M* Y*",
- "DD -- MM -- Y*"};
- // CONSTRUCTORS
-
- public Tstamp(Date date){
- this.setTstamp(date);}
-
- public Tstamp( symjava.sql.Timestamp tstamp){
- nanosec=tstamp.getNanos();
- this.setTstamp(tstamp);}
-
- public Tstamp()
- {this(new Date());}
-
- public Tstamp(String date)
- {this.readDate(date,"MDYhmsn");}
-
- public Tstamp(String date,String format)
- {this.readDate(date,format);}
-
-
- // SETTERS
-
- public void setTstamp(String date)
- {this.readDate(date,m_EntryFormat);}
-
- public void setTstamp(String date, String format)
- {this.readDate(date,format);}
-
- public void setTstamp(Date date){
- day=date.getDate();
- month=date.getMonth();
- year=date.getYear()+1900;
- hour=date.getHours();
- minute=date.getMinutes();
- second=date.getSeconds();
- }
-
- public void setinequalout()
- {setEntryFormat(getDisplayFormat());}
-
- public void setDisplayFormat(String dispfmt)
- {m_DisplayFormat = dispfmt;
- setinequalout();}
-
- public void setEntryFormat(String entfmt)
- {m_EntryFormat = entfmt;}
-
-
- // GETTERS
-
- public String getEntryFormat()
- {return (m_EntryFormat);}
-
- public String getDisplayFormat()
- {return (m_DisplayFormat);}
-
- public String getTstamp()
- {return(this.exTstamp(m_DisplayFormat));}
-
- public String getTstamp(int num)
- {return(this.exTstamp(WAYS[num]));}
-
- public String getTstamp(String exform)
- {return(this.exTstamp(exform));}
-
- public void printTstamp()
- {setText(getTstamp(m_DisplayFormat));}
-
- public Date getDate()throws IllegalArgumentException{
- if(1970>year || year>2038){
- throw new IllegalArgumentException("Date out of range");}
- else return(new Date(year-1900,month,day,hour,minute,second));}
-
- public Timestamp getTimestamp()throws IllegalArgumentException{
- if(1970>year || year>2038){
- throw new IllegalArgumentException("Year out of range for Timestamp");}
- else return(new Timestamp(year-1900,month,day,hour,minute,second,nanosec));}
-
-
-
-
- protected void raiseException(String text)
- {System.out.println(text);}
-
- protected void readDate(String date,String format){
-
- boolean douze=false;
- String tformat="";
- String sub="";
- int value=0;
- int pos=0;
- int act=0;
- int elem=0;
- int count=0;
- date+=" ";
-
- if(format.length()>2){
- for(int x=0;x<format.length()-1;x++){
- try{sub=format.substring(x,x+2);}
- catch(StringIndexOutOfBoundsException e){};
- if(sub.equals("DD")||sub.equals("D%"))tformat+="D";
- if(sub.equals("MM")||sub.equals("M%")||sub.equals("M+")||sub.equals("M*"))tformat+="M";
- if(sub.equals("YY")||sub.equals("Y*"))tformat+="Y";
- if(sub.equals("HH")||sub.equals("H%")||sub.equals("hh")||sub.equals("h%"))tformat+="h";
- if(sub.equals("m%")||sub.equals("mm"))tformat+="m";
- if(sub.equals("s%")||sub.equals("ss"))tformat+="s";
- if(sub.charAt(0)=='n')tformat+="n";
- }
- if(tformat.length()>1)format=tformat;
- }
-
- while (elem<format.length()&&pos<date.length()){
-
- if('0'<=date.charAt(pos) && date.charAt(pos)<='9'&& act==0){
- while('0'<=date.charAt(pos) && date.charAt(pos)<='9'){
- value=value*10+date.charAt(pos)-'0';
- pos++;count++;act=1;
-
- if((format.charAt(elem)=='M' ||format.charAt(elem)=='D'||format.charAt(elem)=='h'
- ||format.charAt(elem)=='m'||format.charAt(elem)=='s')&&(count>1))break;
- if((format.charAt(elem)=='Y')&&(count>3))break;
-
- }}
- if(format.charAt(elem)=='n'){
- for(int j=0;j<9-count;j++){
- value=value*10;}}
-
- if(act==0 ){
- if('0'>date.charAt(pos) || date.charAt(pos)>'9'){
- for(int x=0;x<nMON.length;x++){
- if(nMON[x].regionMatches(true,0,date,pos,3) && format.charAt(elem)=='M'){
- value=x+1;
- pos++;act=1;}
- }
- if("PM".regionMatches(true,0,date,pos,2)){
- douze=true;
- }
- pos++;
- }}
-
- if(format.charAt(elem)=='M')month=value-1;
- if(format.charAt(elem)=='D')day=value;
- if(format.charAt(elem)=='Y'){year=value;
- if (year>=70&&year<101)year+=1900;
- if (year<70)year+=2000;}
- if(format.charAt(elem)=='h')hour=value;
- if(format.charAt(elem)=='m')minute=value;
- if(format.charAt(elem)=='s')second=value;
- if(format.charAt(elem)=='n')nanosec=value;
- if (douze && hour<12)hour=hour+12;
- if(act!=0){elem++;count=0;act=0;value=0;}
-
- }
- while(pos<date.length()){
- if("PM".regionMatches(true,0,date,pos,2)){
- if(hour<12)hour+=12;}
- pos++;
- }
- if(month>11)month=11;
- if(month<0)month=0;
- if (month==1 && day>=29){
- if(calculday(29,1,year)==calculday(1,2,year)){day=28;}
- else{day=29;}}
- else if(day>(moffset[month+1]-moffset[month]))day=(moffset[month+1]-moffset[month]);
- if(day<1)day=1;
- if(hour>23)hour=23;
- if(hour<0)hour=0;
- if(minute>59)minute=59;
- if(minute<0)minute=0;
- if(second>59)second=59;
- if(second<0)second=0;
-
- }
-
- protected String exTstamp(String exform){
- int pos=0;
- int actpos=pos;
- int amhour=hour;
- if(hour>11)amhour=hour-12;
- int nansize=0;
- String snan="";
- String output="";
- exform+=" ";
- while (pos<exform.length()){
- actpos=pos;
-
- for(int x=0;x<DAY.length;x++){
- if(DAY[x].regionMatches(0,exform,pos,DAY[x].length())){
- pos+=DAY[x].length()-1;
- if(DAY[x]=="D%")output+=Integer.toString(day);
- if(DAY[x]=="DD"){
- if(day<10)output+="0";
- output+=Integer.toString(day);}
-
- if(DAY[x]=="D+")output+=nDAY[calculday(day,month,year)];
- if(DAY[x]=="D*")output+=NDAY[calculday(day,month,year)];
- if(DAY[x]=="TH"){
- if(day==1||day==21)output+="st";
- if(day==2||day==22)output+="nd";
- if(day==3||day==23)output+="rd";
- if(day>=4&&day!=21&&day!=22&&day!=23)output+="th";}
- }}
-
- for(int x=0;x<MON.length;x++){
- if(MON[x].regionMatches(0,exform,pos,MON[x].length())){
- pos+=MON[x].length()-1;
- if(MON[x]=="M%")output+=Integer.toString(month+1);
- if(MON[x]=="MM"){
- if(month+1<10)output+="0";
- output+=Integer.toString(month+1);}
- if(MON[x]=="M*")output+=NMON[month];
- if(MON[x]=="M+")output+=nMON[month];
- }}
-
- for(int x=0;x<YEA.length;x++){
- if(YEA[x].regionMatches(0,exform,pos,YEA[x].length())){
- pos+=YEA[x].length()-1;
- if(YEA[x]=="YY")output+=Integer.toString(year-(year/100)*100);
- if(YEA[x]=="Y*")output+=Integer.toString(year);
- }}
- for(int x=0;x<HOU.length;x++){
- if(HOU[x].regionMatches(0,exform,pos,HOU[x].length())){
- pos+=HOU[x].length()-1;
- if(HOU[x]=="h%")output+=Integer.toString(hour);
- if(HOU[x]=="hh"){
- if(hour<10)output+="0";
- output+=Integer.toString(hour);}
- if(HOU[x]=="H%")output+=Integer.toString(amhour);
- if(HOU[x]=="HH"){
- if(amhour<10)output+="0";
- output+=Integer.toString(amhour);}
- if(HOU[x]=="AM"){if(hour>11)output+="PM";
- if(hour<12)output+="AM";}
- }}
-
- for(int x=0;x<MIN.length;x++){
- if(MIN[x].regionMatches(0,exform,pos,MIN[x].length())){
- pos+=MIN[x].length()-1;
- if(MIN[x]=="m%")output+=Integer.toString(minute);
- if(MIN[x]=="mm"){
- if(minute<10)output+="0";
- output+=Integer.toString(minute);}
-
- }}
- for(int x=0;x<SEC.length;x++){
- if(SEC[x].regionMatches(0,exform,pos,SEC[x].length())){
- pos+=SEC[x].length()-1;
- if(SEC[x]=="s%")output+=Integer.toString(second);
- if(SEC[x]=="ss"){
- if(second<10)output+="0";
- output+=Integer.toString(second);}
-
- }}
- for(int x=0;x<NAN.length;x++){
- if(NAN[x].regionMatches(0,exform,pos,NAN[x].length())){
- pos+=NAN[x].length();
- nansize=(int)(exform.charAt(pos)-'0');
- snan=Integer.toString(nanosec+1000000000);
- if(NAN[x]=="n")output+=snan.substring(1,nansize+1);
- }}
- if(actpos==pos)output+=exform.charAt(pos);
- pos++;
- }
- return(output);
- }
-
- private int moffset[]={0,31,59,90,120,151,181,212,243,273,304,334,365};
- public int calculday(int day,int month,int year){
- int yearn=year-1900;
- int tday=(day+4+moffset[month]+((yearn & 3) !=0||yearn%100==0&&(yearn+300)%400!=0||month<2?-1:0)
- +(yearn-70)*365
- +(yearn-69)/4
- -(yearn-1)/100
- +(yearn +299)/400);
- return(tday%7);
- }
-
- public boolean handleEvent(Event event) {
- if(event.id==Event.ACTION_EVENT){
- setTstamp((String)event.arg,m_EntryFormat);
- printTstamp();
- }
- return super.handleEvent(event);
- }}
-
-
-